<?php
/* --------------------------------------------------------------
   PropertiesDataAgent.php 2012-02-14 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2011 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

class PropertiesDataAgent 
{
	function PropertiesDataAgent() 
	{
	}

	#	$coo_properties_data_agent = MainFactory::create_object('PropertiesDataAgent');
	#	$coo_properties_data_agent->rebuild_properties_index();
	function rebuild_properties_index($p_products_id)
	{
		# remove old index content
		$t_sql = '
			DELETE FROM products_properties_index
			WHERE
				products_id = "'. (int)$p_products_id .'"
		';
		xtc_db_query($t_sql);

		# get shop languages
		$t_sql = 'SELECT languages_id FROM languages';
		$t_result = xtc_db_query($t_sql);

		# build index content
		while(($t_row = xtc_db_fetch_array($t_result) ))
		{
			$c_language_id = (int)$t_row['languages_id'];

			$t_sql = '
				INSERT INTO products_properties_index
				SELECT
					ppc.products_id AS products_id,
					pvd.language_id AS language_id,
					pd.properties_id AS properties_id,
					ppc.products_properties_combis_id AS products_properties_combis_id,
					pvd.properties_values_id AS properties_values_id,
					pd.properties_name AS properties_name,
					p.sort_order AS properties_sort_order,
					pvd.values_name AS values_name,
					pv.sort_order AS value_sort_order
				FROM
					properties AS p
						LEFT JOIN properties_values AS pv USING (properties_id)
						LEFT JOIN products_properties_combis_values AS ppcv USING (properties_values_id)
						LEFT JOIN products_properties_combis AS ppc USING (products_properties_combis_id)
						LEFT JOIN properties_description AS pd ON (pv.properties_id = pd.properties_id)
						LEFT JOIN properties_values_description AS pvd ON (pv.properties_values_id = pvd.properties_values_id)
				WHERE
					ppc.products_id = "'. (int)$p_products_id .'" AND
					pd.language_id 	= "'. (int)$c_language_id .'" AND
					pvd.language_id = "'. (int)$c_language_id .'"
				ORDER BY
					properties_sort_order ASC,
					value_sort_order ASC
			';
			xtc_db_query($t_sql);
		}
		return true;
	}
	
	function get_shop_languages_data() 
	{
		# taken from admin/includes/functions/general.php:580: function xtc_get_languages()
		$languages_query = xtc_db_query("select languages_id, name, code, image, directory from ".TABLE_LANGUAGES." order by sort_order");
		while ($languages = xtc_db_fetch_array($languages_query)) {
			$languages_array[] = array (
									'id' => $languages['languages_id'], 
									'name' => $languages['name'], 
									'code' => $languages['code'], 
									'image' => $languages['image'], 
									'directory' => $languages['directory']
								);
		}
		return $languages_array;
	}	
	
	/* ########## properties ########## */
	
	function get_properties_name($p_properties_id, $p_language_id)
	{
		$c_properties_id = (int)$p_properties_id;
		$c_language_id = (int)$p_language_id;
		
		$coo_data = new GMDataObject(
							'properties_description',
							array(
								'properties_id' => $c_properties_id, 
								'language_id' => $c_language_id
							)
						);
						
		$t_name = $coo_data->get_data_value('properties_name');
		return $t_name;
	}
        
        function get_properties_admin_name($p_properties_id, $p_language_id)
	{
		$c_properties_id = (int)$p_properties_id;
		$c_language_id = (int)$p_language_id;
		
		$coo_data = new GMDataObject(
							'properties_description',
							array(
								'properties_id' => $c_properties_id, 
								'language_id' => $c_language_id
							)
						);
						
		$t_name = $coo_data->get_data_value('properties_admin_name');
		return $t_name;
	}
	
	
	function get_all_properties_ids()
	{
		$coo_data = new GMDataObjectGroup('properties', false, array('sort_order'));
		$coo_data->init();
		
		$coo_data_array = $coo_data->get_data_objects_array();
		
		$t_properties_ids_array = array();
		
		for($i=0; $i<sizeof($coo_data_array); $i++)
		{
			$t_properties_ids_array[] = $coo_data_array[$i]->get_data_value('properties_id');
		}
		return $t_properties_ids_array;
	}
	
	function get_properties_full_struct($p_properties_id, $p_language_id=false, $p_prop_values=true)
	{
		$c_properties_id = (int)$p_properties_id;
		$c_language_id = (int)$p_language_id;
		
		$coo_struct_supplier = MainFactory::create_object('PropertiesStructSupplier');
		
		# properties_struct
		$t_full_struct = $coo_struct_supplier->get_properties_struct($c_properties_id, $c_language_id);
		
		# attach optional properties_values_struct
		if($p_prop_values == true)
		{
			$t_properties_values_struct = $coo_struct_supplier->get_properties_values_struct($c_properties_id, $c_language_id);
			$t_full_struct = array_merge(
									$t_full_struct, 
									$t_properties_values_struct);
		}
		return $t_full_struct;
	}
	
	
	/* ########## products_properties_combis ########## */

	
	function get_properties_combis_details($p_combis_id, $p_language_id)
	{
		$t_details_array = array();
		
		$c_combis_id 	= (int)$p_combis_id;
		$c_language_id 	= (int)$p_language_id;
		
		$t_sql = '
			SELECT *
			FROM products_properties_combis_values AS cv
				LEFT JOIN properties_values AS pv USING (properties_values_id)
				LEFT JOIN properties_description AS pd USING (properties_id)
				LEFT JOIN properties_values_description AS pvd ON (pv.properties_values_id = pvd.properties_values_id)
			WHERE
				cv.products_properties_combis_id 	= "'. $c_combis_id	 .'" AND
				pd.language_id 						= "'. $c_language_id .'" AND
				pvd.language_id 					= "'. $c_language_id .'"
		';
		$t_result = xtc_db_query($t_sql);
		
		while(($t_row = xtc_db_fetch_array($t_result) ))
		{
			$t_details_array[] = $t_row;
		}
		return $t_details_array;
	}


	function get_properties_combis_vpe_details($p_combis_id, $p_language_id)
	{
		$t_details_array = array();

		$c_combis_id 	= (int)$p_combis_id;
		$c_language_id 	= (int)$p_language_id;

		# get combis details
		$t_sql = '
			SELECT
				ppc.combi_price_type AS combi_price_type,
				ppc.combi_price AS combi_price,
				ppc.vpe_value AS vpe_value,
				ppc.products_vpe_id AS products_vpe_id
			FROM
				products_properties_combis AS ppc
			WHERE
				ppc.products_properties_combis_id	= "'. $c_combis_id	 .'" 
		';
		$t_result = xtc_db_query($t_sql);

		if(xtc_db_num_rows($t_result) == 0) {
			if(is_object($GLOBALS['coo_debugger'])) $GLOBALS['coo_debugger']->log('get_properties_combis_vpe_details() given combis_id not found: '. $c_combis_id, 'warning');
			return false;
		}
		$t_details_array = xtc_db_fetch_array($t_result);
		$t_details_array['products_vpe_name'] = '';
		
		# get vpe details
		if($t_details_array['products_vpe_id'] > 0) {
			$t_sql = '
				SELECT
					pvpe.products_vpe_name AS products_vpe_name
				FROM
					products_vpe AS pvpe
				WHERE
					pvpe.products_vpe_id	= "'. $t_details_array['products_vpe_id'] .'" AND
					pvpe.language_id		= "'. $c_language_id .'"
			';
			$t_result = xtc_db_query($t_sql);
			
			if(xtc_db_num_rows($t_result) == 0) {
				if(is_object($GLOBALS['coo_debugger'])) $GLOBALS['coo_debugger']->log('get_properties_combis_vpe_details() needed products_vpe_id/language_id not found: '. $t_details_array['products_vpe_id'].'/'.$c_language_id, 'warning');
			} else {
				# attach products_vpe_name
				$t_details_array['products_vpe_name'] = mysql_result($t_result, 0, 'products_vpe_name');
			}
		}
		return $t_details_array;
	}

	
	
	function get_all_properties_combis_ids($p_products_id)
	{
		$c_products_id = (int)$p_products_id;
		$t_language_id = (int)$_SESSION['languages_id'];

		$t_properties_combis_ids_array = array();

		$coo_product_properties_data = MainFactory::create_object('ProductPropertiesData', array($c_products_id, $t_language_id));
		$t_combis_array = $coo_product_properties_data->get_properties_combis_array();

		foreach($t_combis_array as $t_combis_item_array)
		{
			$t_properties_combis_ids_array[] = $t_combis_item_array['products_properties_combis_id'];
		}

		# GM_MOD: ratscheu-fix
		/*
		$coo_data = new GMDataObjectGroup('products_properties_combis', array('products_id' => $c_products_id), array('sort_order'));
		$coo_data_array = $coo_data->get_data_objects_array();
		
		$t_properties_combis_ids_array = array();
		
		for($i=0; $i<sizeof($coo_data_array); $i++)
		{
			$t_properties_combis_ids_array[] = $coo_data_array[$i]->get_data_value('products_properties_combis_id');
		}
		*/
//		print_r($t_properties_combis_ids_array);
		return $t_properties_combis_ids_array;
	}
	
	function get_combis_full_struct($p_properties_combis_id, $p_language_id)
	{
		$t_combis_struct = array();
		
		$c_properties_combis_id = (int)$p_properties_combis_id;
		$c_language_id = (int)$p_language_id;
		
		# main data, properties_combis
		$coo_data = new GMDataObject('products_properties_combis', array('products_properties_combis_id' => $c_properties_combis_id));
				
		$t_combis_struct['products_properties_combis_id'] = $coo_data->get_data_value('products_properties_combis_id');
		$t_combis_struct['products_id'] = $coo_data->get_data_value('products_id');
		$t_combis_struct['sort_order'] = $coo_data->get_data_value('sort_order');
		$t_combis_struct['combi_model'] = $coo_data->get_data_value('combi_model');
		$t_combis_struct['combi_quantity_type'] = $coo_data->get_data_value('combi_quantity_type');
		$t_combis_struct['combi_quantity'] = (float)$coo_data->get_data_value('combi_quantity');
		$t_combis_struct['combi_shipping_status_id'] = (int)$coo_data->get_data_value('combi_shipping_status_id');
		$t_combis_struct['combi_shipping_status_name'] = '';
		$t_combis_struct['combi_shipping_status_image'] = '';
		$t_combis_struct['combi_weight'] = $coo_data->get_data_value('combi_weight');
		$t_combis_struct['combi_price_type'] = $coo_data->get_data_value('combi_price_type');
		$t_combis_struct['combi_price'] =  number_format($coo_data->get_data_value('combi_price'), 2);
		$t_combis_struct['combi_image'] = $coo_data->get_data_value('combi_image');
		$t_combis_struct['vpe_value'] = (float)$coo_data->get_data_value('vpe_value');
		$t_combis_struct['products_vpe_id'] = $coo_data->get_data_value('products_vpe_id');
		$t_combis_struct['products_vpe_name'] = '';
		
		if($coo_data->get_data_value('combi_shipping_status_id') > 0)
		{
			$t_parameters_array = array(
									'shipping_status_id' => $coo_data->get_data_value('combi_shipping_status_id'),
									'language_id' => $_SESSION['languages_id']
								);
			$coo_shipping_status_do = new GMDataObject('shipping_status', $t_parameters_array);
			$t_combis_struct['combi_shipping_status_name'] = $coo_shipping_status_do->get_data_value('shipping_status_name');
			$t_combis_struct['combi_shipping_status_image'] = $coo_shipping_status_do->get_data_value('shipping_status_image');
		}

		if($coo_data->get_data_value('products_vpe_id') > 0)
		{
			$t_parameters_array = array(
									'products_vpe_id' => $coo_data->get_data_value('products_vpe_id'),
									'language_id' => $_SESSION['languages_id']
								);
			$coo_vpe_do = new GMDataObject('products_vpe', $t_parameters_array);
			$t_combis_struct['products_vpe_name'] = $coo_vpe_do->get_data_value('products_vpe_name');
		}

		
		$coo_combis_struct_supplier = MainFactory::create_object('PropertiesCombisStructSupplier');
		$t_combis_values_struct = $coo_combis_struct_supplier->get_properties_combis_struct($c_properties_combis_id, $c_language_id);
		
		for($i=0; $i<sizeof($t_combis_values_struct); $i++)
		{
			$t_properties_name = $this->get_properties_name(
											$t_combis_values_struct[$i]['properties_id'], 
											$_SESSION['languages_id']
										);
                        
                        $t_properties_admin_name = $this->get_properties_admin_name(
											$t_combis_values_struct[$i]['properties_id'], 
											$_SESSION['languages_id']
										);
			$t_combis_values_struct[$i] = array_merge(
												$t_combis_values_struct[$i],
												array('properties_name' => $t_properties_name, 'properties_admin_name' => $t_properties_admin_name)
											);
		}
		$t_combis_struct['COMBIS_VALUES'] = $t_combis_values_struct;
		
		return $t_combis_struct;
	}
	
}

?>